package com.project.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.project.bean.EmployeeBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.time.LocalDate;
import java.util.Enumeration;

@Controller
public class EmServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        EmployeeBean em = new EmployeeBean(
                //req.getParameter("name"),
                //req.getParameter("sex"),
                //req.getParameter("job"),
                //LocalDate.parse(req.getParameter("birthday")),
                //Integer.parseInt(req.getParameter("money")),
                //req.getParameter("edu"),
                //req.getParameterValues("like")
        );
        this.fullBean(em,req);
        resp.getWriter().println(em);
        PrintWriter out = resp.getWriter();
        out.println("<hr>");
        //将java数据对象转化为json格式
        ObjectMapper om = new ObjectMapper();
        //提供新日期类型的支持
        om.registerModule(new JavaTimeModule());
        String jsonStr = om.writeValueAsString(em);
        out.println(jsonStr);
        out.write("<hr>");
        om.writeValue(resp.getWriter(),em);
    }

    private void fullBean(Object beanObj,HttpServletRequest req){
        Enumeration<String> em = req.getParameterNames();
        Class beanClass = beanObj.getClass();
//        通过反射来封装--方法一
//               req.getParameter("name"),
//                req.getParameter("sex"),
//                req.getParameter("job"),
//                Integer.parseInt(req.getParameter("money")),
//                LocalDate.parse(req.getParameter("birthday")),
//                req.getParameter("edu"),
//                req.getParameterValues("like")
//        方法二
        while(em.hasMoreElements()){//判断是否有下一个元素
            //取出下一个元素
            String str = em.nextElement();
            //得到表单数据
            String value = req.getParameter(str);

            try {
                //得到属性对象
                Field f = beanClass.getDeclaredField(str);
                f.setAccessible(true);

                if(f.getType() == String.class) {
                    //填充属性值
                    f.set(beanObj, value);
                }
                else if(f.getType() == int.class || f.getType() == Integer.class){
                    f.set(beanObj,Integer.parseInt(value));
                }
                else if(f.getType() == double.class || f.getType() == Double.class){
                    f.set(beanObj,Double.parseDouble(value));
                }
                else if(f.getType() == LocalDate.class){
                    f.set(beanObj,LocalDate.parse(value));
                }
                else if(f.getType() == String[].class){
                    f.set(beanObj,req.getParameterValues(str));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
